package com.seari.custody.service.Impl;

import com.seari.custody.bean.*;
import com.seari.custody.constant.BusinessConstant;
import com.seari.custody.mapper.ProjectManageMapper;
import com.seari.custody.service.ProjectManageService;
import com.seari.custody.service.ProjectService;
import com.seari.custody.util.HandlerNullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ProjectManageServiceImpl implements ProjectManageService {

    @Value("${minio.url}")
    private String minioUrl;//minio 查看pdf地址

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Autowired
    private ProjectManageMapper projectManageMapperMapper;

    @Autowired
    private ProjectService projectService;


    @Override
    public List<Map<String, String>> queryProListByProperty(String versionType,String selectType,String userNumber, String sort ,String projectNameSelect,String startTime,String endTime,String proID) {
        return projectManageMapperMapper.queryProListByProperty(versionType, selectType, userNumber, sort, projectNameSelect,startTime,endTime,proID);
    }

    @Override
    public Project queryProByProID(String proID) {
        return projectManageMapperMapper.queryProByProID(proID);
    }

    @Override
    public List<Project> queryProByProNO(String proNO) {
        return projectManageMapperMapper.queryProByProNO(proNO);
    }

    @Override
    public List<TechnicalAdvice> queryTechnicalAdviceByProID(String proID, String reviewStatus) {
        return projectManageMapperMapper.queryTechnicalAdviceByProID(proID, reviewStatus);
    }

    @Override
    public List<SpecialProtection> querySpecialProtectionByProID(String proID) {
        return projectManageMapperMapper.querySpecialProtectionByProID(proID);
    }

    @Override
    public Project queryProDetailsByProID(String proID) {
        Project p = projectManageMapperMapper.queryProByProID(proID);

        if(!HandlerNullUtils.objectIsNull(p)){
            p.setReviewStatus(BusinessConstant.reviewStatusMap.get(p.getReviewStatus()));
            p.setStatus(BusinessConstant.flowStatusMap.get(p.getFlowStatus()));
            p.setType(BusinessConstant.xkTypeMap.get(p.getType()));
            if(!HandlerNullUtils.objectIsNull(p.getLineType())){
                String[] strList = p.getLineType().split(",");
                StringBuilder s = new StringBuilder();

                for(int i = 0;i<strList.length;i++){
                    s.append(BusinessConstant.lineTypeMap.get(strList[i]));
                    s.append(",");
                }
                p.setLineType(s.toString().substring(0,s.length()-1));
            }

            if(!HandlerNullUtils.objectIsNull(p.getStationFlag())){
                String[] strList = p.getStationFlag().split(",");
                StringBuilder s = new StringBuilder();

                for(int i = 0;i<strList.length;i++){
                    s.append(BusinessConstant.stationFlagMap.get(strList[i]));
                    s.append(",");
                }
                p.setStationFlag(s.toString().substring(0,s.length()-1));
            }

            if(!HandlerNullUtils.objectIsNull(p.getStationArea())){
                String[] strList = p.getStationArea().split(",");
                StringBuilder s = new StringBuilder();

                for(int i = 0;i<strList.length;i++){
                    s.append(BusinessConstant.stationAreaMap.get(strList[i]));
                    s.append(",");
                }
                p.setStationArea(s.toString().substring(0,s.length()-1));
            }
        }
        return p;
    }

    @Override
    public List<File> getProFileByProID(String proID,List<String> flowNum,List<String> outLineNum) {
        List<File> filePdf = new ArrayList<File>();
        List<File> fileWord = new ArrayList<File>();
        List<File> othersfile = new ArrayList<File>();
        List<File> fileRe = new ArrayList<File>();
        List<File> files = projectManageMapperMapper.getProFileByProID(proID, flowNum, outLineNum);

        for(File f : files){

            if(f.getFiletype().contains("工程信息概况")&&(f.getExtName().equals("pdf")||f.getExtName().equals("PDF")||f.getExtName().equals("docx"))){
                //给前台标识 工程信息概况
                f.setFileTypeCode("1");
                f.setUrl(minioUrl+f.getUrl());
                if(f.getExtName().equals("pdf")||f.getExtName().equals("PDF")){
                    filePdf.add(f);
                }else{
                    fileWord.add(f);
                }

            }else{
                f.setFileTypeCode("0");
                othersfile.add(f);
            }
        }

        if (filePdf.size()>1){
            Optional<File> filePdfMax = filePdf.stream().max(Comparator.comparing(File ::getFileEmcId));
            fileRe.add(filePdfMax.get());
        }else{
            fileRe.addAll(filePdf);
        }

        if (fileWord.size()>1){
            Optional<File> fileWordMax = fileWord.stream().max(Comparator.comparing(File ::getFileEmcId));
            fileRe.add(fileWordMax.get());
        }else{
            fileRe.addAll(fileWord);
        }

        fileRe.addAll(othersfile);
        return fileRe;
    }

    @Override
    public List<Map<String, String>> queryTechnicalAdviceFileByForProManage(String proID, String reviewStatus) {
        return projectManageMapperMapper.queryTechnicalAdviceFileByForProManage(proID, reviewStatus);
    }

    @Override
    public List<Map<String, String>> querySpecialProtectionFileByForProManage(String proID) {
        return projectManageMapperMapper.querySpecialProtectionFileByForProManage(proID);
    }

    @Override
    public CreateFolder insertCreateFolder(String folderName,String userNumber) {
        List<Map<String, String>> list=projectManageMapperMapper.queryProListByProperty(BusinessConstant.VERSION_TYPE_NEW, null, null, null, folderName, null, null,null);
        CreateFolder createFolder = new CreateFolder();
        if(list.size()>0){
            createFolder.setId(-1);
            return createFolder;
        }
        createFolder.setName(folderName);
        createFolder.setStatus("1");
        createFolder.setCreateUser(userNumber);
        createFolder.setCreateTime(sdf.format(new Date()));
        createFolder.setUpdateUser(userNumber);
        createFolder.setUpdateTime(sdf.format(new Date()));
        projectManageMapperMapper.insertCreateFolder(createFolder);

        //向project表插入数据
        projectService.createProjectByProjectName(folderName, createFolder.getId().toString());

        return createFolder;
    }

    @Override
    public void insertOtherFile(OtherFile otherFile) {
        projectManageMapperMapper.insertOtherFile(otherFile);
    }

    @Override
    public List<OtherFile> queryOtherFileByFolderID(List<String> idList,String fileName) {
        return projectManageMapperMapper.queryOtherFileByFolderID(idList,fileName);
    }

    @Override
    public CreateFolder queryFolderByID(String folderID) {
        return projectManageMapperMapper.queryFolderByID(folderID);
    }

    @Override
    public List<CreateFolder> queryFolderByProID(String proID) {
        return projectManageMapperMapper.queryFolderByProID(proID);
    }

    @Override
    public void updateCreateFolder(CreateFolder updFolder) {
        projectManageMapperMapper.updateCreateFolder(updFolder);
    }

    @Override
    public void updateOtherFile(OtherFile otherFile) {
        projectManageMapperMapper.updateOtherFile(otherFile);
    }

    @Override
    public OtherFile queryFileByID(String id) {
        return projectManageMapperMapper.queryFileByID(id);
    }


    @Override
    public List<String> queryOutlineNumber(String proID) {
        return projectManageMapperMapper.queryOutlineNumber(proID);
    }

    @Override
    public List<String> queryFlowNumber(String proID) {
        return projectManageMapperMapper.queryFlowNumber(proID);
    }

    @Override
    public List<Map<String, String>> queryFlowStatus(String type, String proID, String flow, String num) {
        return projectManageMapperMapper.queryFlowStatus(type, proID, flow, num);
    }

    @Override
    public Map<String, String> queryFristFlow(String flowNum, String proID,String outLineNum) {
        return projectManageMapperMapper.queryFristFlow(flowNum, proID,outLineNum);
    }

}
